package org.zfes.snowier.zbss.xzy.router.device.message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.zfes.snowier.web.controller.BaseController;
import org.zfes.snowier.web.util.FileResInfo;
import org.zfes.snowier.web.util.UploadUitl;
import org.zfes.snowier.zbss.xzy.base.HosConst;
import org.zfes.snowier.zbss.xzy.biz.message.service.IMessageService;
import org.zfes.snowier.zbss.xzy.biz.vedio.model.CloudImage;
import org.zfes.snowier.zbss.xzy.biz.vedio.service.ICloudImageService;
import org.zfes.snowier.zbss.xzy.biz.vedio.service.ICloudVedioService;
import org.zfes.snowier.zbss.xzy.message.MessagePusDto;
import org.zfes.snowier.zbss.xzy.sms.SmsTencentSender;
import org.zfes.snowier.zbss.xzy.tvedio.CloudCoverCreateDto;
import org.zfes.snowier.zbss.xzy.tvedio.CloudVedioCreateDto;

import com.google.common.collect.Lists;

import io.swagger.annotations.ApiOperation;

import org.zfes.snowier.common.websocket.WebsocketMsgHandler;
import org.zfes.snowier.core.data.JSONMSG;
import org.zfes.snowier.core.util.UUIDUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
@Controller 
@RequestMapping("/api/device/message")
public class MessageController implements BaseController {


	@Autowired
	WebsocketMsgHandler websocketMsgHandler;
	
	@Autowired
	private IMessageService messageService;
	
	@Autowired
	private ICloudVedioService cloudVedioService;

	@Autowired
	private ICloudImageService cloudImageService;
	@Autowired
	private SmsTencentSender smsTencentSender;
	
	  @ApiOperation(value = "报警信息上报")
      @RequestMapping(value="/alertMsg", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
      public  @ResponseBody Object alertMsg(@RequestBody MessagePusDto message){
    	Long messageId= messageService.alertMsg(message);
    	//System.out.println("--alertMsg--"+message);
    	//smsTencentSender.sendSms(Lists.newArrayList("+8615969701578"),message.getDeviceNum(), message.getContent());
    	
    	websocketMsgHandler.sendToUser("1",new JSONMSG(200)
    			.setWsfun("onMessage")
    			.put("messageContent", message.getContent())
    			.put("messageId", messageId)
    			.put("deviceNum", message.getDeviceNum()));
    	
    	return ajaxDoneSuccess("数据操作成功 ").put("messageId", messageId);
      }
    
	  @ApiOperation(value = "报警信息-视频证据上报")
	  @RequestMapping(value="/uploadMsgVedio/{messageId}/{deviceNum}", 
	  					  method = RequestMethod.POST,
						  produces = MediaType.APPLICATION_JSON_VALUE,
						  consumes = { "application/x-www-form-urlencoded" } ,
						  headers = "content-type=multipart/form-data")
	  public  @ResponseBody Object uploadMsgVedio(@RequestParam("files") MultipartFile[] files,@PathVariable("messageId") Long messageId,@PathVariable("deviceNum") String deviceNum){
		  ZAssert.notNull(messageId, "消息ID不能为空");
		  ZAssert.hasText(deviceNum, "设备编号不能为空");
		  if(files==null||files.length!=2) {
			  ZAlert.throwSLE("文件个数错误");
		  }
		  
		  MultipartFile vedioFile=null;
		  MultipartFile coverFile=null;
		  
		  if(".jpg".equalsIgnoreCase(UploadUitl.getFileType(files[0].getOriginalFilename()))) {
			  vedioFile=files[1];
			  coverFile=files[0];
		  }else {
			  vedioFile=files[0];
			  coverFile=files[1];
		  }
		  
		  FileResInfo fileInfoVideo =  UploadUitl.tranferFile(vedioFile,  HosConst.EVIDENCE_FILE_PATH);
		  FileResInfo fileInfoCover =  UploadUitl.tranferFile(coverFile,  HosConst.EVIDENCE_FILE_PATH);
		  
		  CloudVedioCreateDto dto=new CloudVedioCreateDto(deviceNum,messageId)
				  .setCoverFileId(fileInfoCover.getFileId()).setCoverFileName(fileInfoCover.getFileNewName()).setCoverFilePath(fileInfoCover.getAbsolutePath())
				  .setVedioFileId(fileInfoVideo.getFileId()).setVedioFileName(fileInfoVideo.getFileNewName()).setVedioFilePath(fileInfoVideo.getAbsolutePath());
		  
		 Long vedioId=cloudVedioService.uploadMsgVedio(dto);

	    return ajaxDoneSuccess("数据操作成功 ").put("vedioId", vedioId);
	  }
	  
	  @ApiOperation(value = "报警信息-图片证据上传")
	  @RequestMapping(value="/uploadMsgImage/{messageId}/{deviceNum}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	  public  @ResponseBody Object uploadMsgImage(
						  @RequestParam("image") MultipartFile image,
						  @PathVariable("messageId") Long messageId,
						  @PathVariable("deviceNum") String deviceNum){
	    
		  FileResInfo fileInfoImage =  UploadUitl.tranferFile(image,  HosConst.UMAGE_FILE_PATH);
		  CloudImage cloudImage=  new CloudImage( deviceNum,  messageId)
				  .setLocalFilePath(fileInfoImage.getAbsolutePath())
				  .setFileName(fileInfoImage.getFileNewName())
				  .setFileId(fileInfoImage.getFileNewName());
		  
		  Long imageId = cloudImageService.createCloudImage(cloudImage);
		  
	    return ajaxDoneSuccess("数据操作成功 ").put("imageId", imageId);
	  }
	
	  
//	  @ApiOperation(value = "报警信息-视频证据封面上传")
//	  @RequestMapping(value="/uploadMsgVedioCover/{vedioId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
//	  public  @ResponseBody Object uploadMsgVedioCover(@RequestParam("image") MultipartFile image,@PathVariable("vedioId") Long vedioId){
//		  ZAssert.notNull(vedioId, "视频ID不能为空");
//		  FileResInfo fileInfo =  UploadUitl.tranferFile(image,  "evidence");
//		  CloudVedioCoverDto cover=new CloudVedioCoverDto( vedioId, fileInfo.getAbsolutePath(),  fileInfo.getFileNewName(),fileInfo.getFileId());
//		  cloudVedioService.uploadMsgVedioCover(cover);
//
//	    return ajaxDoneSuccess("数据操作成功 ");
//	  }
	
}